<?php
namespace app\api\controller\V22;

use app\api\controller\Base;

class ModeProduct extends Base
{
    // 不需要验证
    protected $excludeValidateCheck = [
        'listsInformation', 'lists'
    ];

    // 定义自动实例化模型
    protected $ModelPath = 'ModeProduct';

    public function listsInformation(){
        //整理数据
        $data = [
            'goods'=>[]
        ];
        //响应数据
        return showSuccess($data);
    }

    public function lists(){
        $params = request()->param();
        $type = request()->param('type', 'mode_one');
        $currentPage = request()->param('page', 1);
//        $type = request()->param('type', 'all');

//        $userId = request()->UserModel->id;
        $map = [];
//        $model = $this->M->with(['ordersGoods']);
//        if($type != 'all')
//        {
//            $map[] = ['status', '=', $type];
//        }
        switch ($type){
            case 'mode_one':
                $map[] = ['type', 'between', [1, 2]];
                break;
            case 'mode_two':
                $map[] = ['type', '=', 3];
                break;
            case 'mode_three':
                $map[] = ['type', '=', 4];
                break;
        }
        $list = $this->M
            ->where($map)
            ->append(['cover', 'alias', 'params'])
            ->order('id ASC')
//            ->limit($currentPage, $this->pageSize)
            ->select();
//            ->paginate($this->pageSize)
//            ->toArray();
        $data = [];
//        $data['total_page'] = (int)ceil($list['total']/$this->pageSize);;
        $data['page_size'] = $this->pageSize;
        $data['page'] = $currentPage;//$list['current_page'];
        $data['lists'] = $list;// $list['data'];

        return showSuccess($data);
    }

    public function detail()
    {
        //获取商品ID
        $id = (int)(request()->param('id', 0));
        //查询商品信息
        if (!$goods_info = $this->M->field('*')->append(['cover_format', 'unit_format', 'content_format'])->findOrEmpty($id)->toArray()) {
            //返回失败
            return showError('商品信息不存在');
        }
        //获取图册
        $images = [];
        //判断信息
        if ($goods_info['img']) {
            //追加内容
            array_unshift($images, $goods_info['cover_format']);
        }
        //整理基本结构
        $data = [
            'id' => $id,
            'title' => $goods_info['title'],
            'albums' => $images,
            'price' => $goods_info['price'],//number_format($goods_info['price'], 2),
            'cate_name' => '',
            'description' => (string)$goods_info['title'] ?: $goods_info['title'],
            'tags' => [],
            'pictures' => $goods_info['content_format'] ?: ($goods_info['cover_format'] ? [$goods_info['cover_format']] : [])
        ];
        //返回数据
        return showSuccess($data);
    }
}